﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FBroSharp.DataType;
using FBroSharp;
using FBroSharp.Lib;
using System.IO;

namespace _005创建独立缓浏览器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //按钮点击事件
        private void BuutonClick(object sender, EventArgs e)
        {
            Button Btn = (Button)sender;
            if (Btn == button_创建浏览器1)
            {
                CreateBrowser(label1.Handle, label1.Width, label1.Height, "Browser1");
                //MessageBox.Show("创建浏览器1");
            }
            else { 
                CreateBrowser(label2.Handle,label2.Width, label2.Height, "Browser2");

                //MessageBox.Show("创建浏览器2");

            }
        }


        public void CreateBrowser(IntPtr PraentHandle,int BrowserWidth, int BroserHeight, string CachePath)
        {
            string url = "https://www.bilibili.com/";
            FBroSharpWindowsInfo windowsInfo = new FBroSharpWindowsInfo
            {
                parent_window = PraentHandle,
                x = 0,
                y = 0,
                width = BrowserWidth,
                height = BroserHeight,
            };
            BrowserEvent browser_event = new BrowserEvent();//浏览器事件

            //独立缓存
            FBroSharpRequestContextSet RequestContextSet = new FBroSharpRequestContextSet {
                cache_path = Directory.GetCurrentDirectory() + $"\\Cache\\{CachePath}\\"
            };
            Console.WriteLine("缓存目录：" + RequestContextSet.cache_path);
            FBroSharpRequestContext RequestContext = (FBroSharpRequestContext)FBroSharpRequestContext.CreateContext(RequestContextSet);

            FBroSharpControl.CreatBrowser(url, windowsInfo, default, RequestContext, default, browser_event, default);

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < BrowserList.data.Count; i++)
            { 
                BrowserList.data[i].MoveWindow(0, 0, label1.Width, label1.Height, true);
            }
        }
    }
}
